<?php /* УТФ-8:fix */

class SPImodel__singlepage__manage extends SPImodel__singlepage
{
    public function get_by_tid( $item_tid )
    {
        $query =    " SELECT * "
               .      " FROM ".SPIcfg::$store['table:singlepages']
               .     " WHERE tid = '" . addslashes( $item_tid ) ."' "
               ;

        return SPIstore::I()->query( $query )->fetch_and_close();
    }

    public function insert( $item_tid, $fields )
    {
        $fields['pid_profile_addby'] = SPI::I('Iam')->get_id();
        $fields['tid'] = $item_tid;

        $result = SPIstore::I()->insert(
            SPIcfg::$store['table:singlepages'],
            $fields,
            null,
            array( 'id' => SPIcfg::$store['sequence:global'] )
        );

        return $result === false ? false : $result['id'];
    }

    public function modify( $item_tid, $fields )
    {
        return SPIstore::I()->modify(
            SPIcfg::$store['table:singlepages'],
            $fields,
            array( 'tid' => $item_tid )
        );
    }

    public function save( $item_tid, $fields )
    {
        if( $this->get_by_tid( $item_tid ) === false )
        {
            return $this->insert( $item_tid, $fields );
        }
        else
        {
            return $this->modify( $item_tid, $fields );
        }
    }

    /*
    public function delete( $item_tid )
    {
        return SPIstore::I()->delete(
            SPIcfg::$store['table:singlepages'],
            array( 'tid' => $item_tid )
        );
    }
    */
}
